<template>
  <div class="cart">
    <!-- 购物车为空提示 -->
    <van-empty
      class="custom-image"
      image="https://img01.yzcdn.cn/vant/custom-empty-image.png"
      description="购物车为空，请选购商品"
      v-if="cart.length === 0"
    />
    <template v-else>
      <!-- 购物车商品 -->
      <van-swipe-cell
        v-for="prod in cart"
        :key="prod.id"
      >
        <van-card
          :price="prod.price | money"
          :desc="prod.desc"
          :title="prod.title"
          class="goods-card"
        >
          <template #thumb>
            <van-checkbox :value="prod.checked" @click="toggleChecked(prod.id)"></van-checkbox>
            <div class="van-image" style="width: 80%; height: 100%;">
              <img :src="prod.image" class="van-image__img" style="object-fit: cover;">
            </div>
          </template>
          <template #num>
            <van-stepper
              :value="prod.amount"
              theme="round"
              button-size="22"
              @change="handleChange(prod.id, $event)"
            />
          </template>
        </van-card>
        <template #right>
          <van-button
            square
            text="删除"
            type="danger"
            class="delete-button"
            @click="handleRemove(prod.id)"
          />
        </template>
      </van-swipe-cell>
    </template>

    <!-- 提交订单 -->
    <van-submit-bar :price="totalMoney" button-text="提交订单" @submit="onSubmit">
      <van-checkbox :value="allChecked" @click="handleCheckAll">全选</van-checkbox>
      <template #tip>
        你的收货地址不支持同城送, <span>修改地址</span>
      </template>
    </van-submit-bar>
  </div>
</template>

<script>
import { mapMutations, mapGetters } from 'vuex'
export default {
  name: 'Cart',
  computed: {
    ...mapGetters(['allChecked', 'totalMoney']),
    cart() {
      return this.$store.state.cart
    },
  },
  methods: {
    ...mapMutations(['remove', 'modifyAmount', 'toggleChecked', 'toggleCheckAll']),
    // 删除
    handleRemove(id) {
      // this.$store.commit('remove', id)
      this.remove(id)
    },
    // 修改数量
    handleChange(id, value) {
      // console.log('修改数量：', value)
      this.modifyAmount({
        id,
        amount: value,
      })
    },
    // 全选
    handleCheckAll() {
      this.toggleCheckAll(this.allChecked)
    },
    // 提交确认订单
    onSubmit() {
      // 编程式的导航
      this.$router.push('/confirm')
    },
  },
}
</script>

<style lang="less" scoped>
  .goods-card {
    margin: 0;
    background-color: #fff;
  }

  .delete-button {
    height: 100%;
  }

  /deep/ .van-card__thumb {
    width: 240px;
    display: flex;
    justify-content: space-between;
  }
</style>
